clear all
clear matrix
version 13
capture log close
set matsize 11000
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------

global date = "2206"
global niter = 15

global stub 	 = "/Volumes/Transcend/AWFP/ThirdChapter_revision"
global main		 = "$stub/data"
global inputcali = "$stub/data/CalibrationInput"	
global outputcali= "$stub/data/CalibrationOutput"	
global data_proc = "$stub/data"
global graphs    = "$stub/graphs"	
global tables    = "$stub/tables"
global do        = "$stub/do"
global excel     = "$stub/excel"	
global orig      = "$stub/orig"

cd "$main"

********************************************************************************
* SETTING GLOBALS **************************************************************
********************************************************************************
global x = 1.64
global artest = 1
global figures = 0
global truncation = 1
global concentrationfile = "qoq_percentiles_72"
global employmentfile = "employmentwidedata"

* Seasonally adjust employment data:
do "$do/SeasonallyAdjustEmploymentQuarterly.do"


* Version denotes the transformation applied to the data - see settingglobals.do 
* for a detailed description

*local listcontrol "1 2 3 5 6"		/* ALL ALTERNATIVE SPECIFICATIONS */
local listcontrol "1"				/* BASELINE ONLY - version 1 needs to be always included */
foreach cc of local listcontrol{
global version = `cc'
do ${do}/settingglobals.do
********************************************************************************
use ranking_72.dta, clear			/* GET NUMBER OF CITIES IN THE ANALYSIS */
sum amr
global N = `r(max)'
********************************************************************************
* RAW DATA VISUALISATION *******************************************************
********************************************************************************
if ${figures}==1{
if ${version}==1{
forv n = 1(1)$N{
use ${orig}/${employmentfile}.dta, clear
qui: tsline employment if amr==`n'
qui: graph save "${graphs}/employment_`n'.gph", replace
use ${orig}/${concentrationfile}.dta, clear
qui: cap ren share_p99 sharep99		
qui: cap ren share_p95 sharep95
qui: cap ren share_p90 sharep90	
qui: xtset amr eventdate
qui: tsline herfindahl if amr==`n'
qui: graph save "${graphs}/concentration`n'_1.gph", replace
qui: tsline sharep99 if amr==`n'
qui: graph save "${graphs}/concentration`n'_2.gph", replace
qui: graph combine "${graphs}/concentration`n'_1.gph" "${graphs}/concentration`n'_2.gph" "${graphs}/employment_`n'.gph"
qui: graph export "${graphs}/concentration and employment/employment_concentration`n'.png", replace
qui: cap erase "${graphs}/concentration`n'_1.gph"  
qui: cap erase "${graphs}/concentration`n'_2.gph" 
qui: cap erase "${graphs}/employment_`n'.gph"
}
}
}
********************************************************************************
* WORK ON CONCENTRATION MEASURE ************************************************
********************************************************************************
di "Seasonally adjust concentration time series *******************************"
if ${version}==1{
use ${orig}/${concentrationfile}.dta, clear
qui: cap ren share_p99 sharep99		
qui: cap ren share_p95 sharep95
qui: cap ren share_p90 sharep90	
xtset amr eventdate
qui: keep sharep99 herfindahl eventdate amr
qui: duplicates drop
qui: reshape wide sharep99 herfindahl, i(eventdate) j(amr)
qui: sort eventdate
qui: gen idtime = _n
qui: tsset eventdate
forv n = 1(1)$N {
di `n'
seasonallyadjust sharep99`n', gen(sharep99_sa`n') ar(1) ma(1) x11 multiplicative replace
seasonallyadjust herfindahl`n', gen(herfindahl_sa`n') ar(1) ma(1) x11 multiplicative replace
}
save ${orig}/${concentrationfile}_new.dta, replace
}
do ${do}/AdjustConcentrationQuarterly.do 
********************************************************************************
* OECD RECESSION DATES *********************************************************
********************************************************************************
qui: import excel RecDatesGermany.xlsx, clear firstrow
qui: replace oecd_dates = 0 if oecd_dates == .

qui: gen month 		= 1 if quarter == 1
qui: replace month 	= 4 if quarter == 2
qui: replace month 	= 7 if quarter == 3
qui: replace month 	= 10 if quarter == 4

qui: gen eventdate = qofd(mdy(month,15,year))
qui: format eventdate %tq

sum
qui: save oecd_dates.dta, replace
********************************************************************************
* WORK ON LOCAL EMPLOYMENT *****************************************************
********************************************************************************
di "Adjust local employment time series ***************************************"
use ${orig}/${employmentfile}.dta, clear

if ${version}==1{
********************************************************************************
* GETS FIGURE ON NATIONAL EMPLOYMENT AND TURNING POINTS ************************
********************************************************************************
qui: do ${do}/FigNational.do
}

qui: keep ${var_y} eventdate amr
qui: reshape wide  ${var_y}, i(eventdate) j(amr)
qui: sort eventdate
qui: gen idtime = _n

forv n = 1(1)$N {
qui: gen log${var_y}`n' = ln(${var_y}`n')	
preserve
qui: keep log${var_y}`n' eventdate idtime
qui: reg log${var_y}`n' idtime
qui: tsset eventdate
qui: estat sbsingle
gen estimatedbreakdate`n'="`r(breakdate)'"
if `n'==1{
qui: keep  estimatedbreakdate`n'
qui: gen aux=1
qui: duplicates drop 
qui: save breakanalysisy${version}.dta, replace
}
else{
qui: keep  estimatedbreakdate`n'
qui: duplicates drop 
qui: gen aux=1
qui: merge 1:1 aux using breakanalysisy${version}.dta
qui: drop _merge
qui: save breakanalysisy${version}.dta, replace
}
restore
}

preserve
qui: use breakanalysisy${version}.dta, clear
qui: reshape long estimatedbreakdate, i(aux) j(amr)
qui: tab estimatedbreakdate
restore

tsset eventdate
forv n = 1(1)$N {
qui: tsfilter hp cycle_hp_${var_y}`n' = log${var_y}`n', smooth(1600) 

qui: reg log${var_y}`n' idtime
qui: mat A = e(b)
qui: gen cycle_linear_${var_y}`n' = log${var_y}`n' - A[1,1]*idtime - A[1,2]
}
********************************************************************************
forv n = 1(1)$N {
qui: ren ${dep_var}`n' aux
qui: sbbq aux
qui: ren aux_point employment_point`n'
qui: ren aux ${dep_var}`n'
}
save localemployment${version}.dta, replace
********************************************************************************
* Testing CG (2019) ************************************************************
********************************************************************************
use qoq_concentration_72_sa_${version}.dta, clear
qui: keep ${indep_var} idtime amr

if "${var_x}"=="herfindahl_sa"{
di "take exponential of x"
qui: replace ${indep_var}=exp(${indep_var})
}

qui: reshape wide ${indep_var}, i(idtime) j(amr)
merge 1:1 idtime using localemployment${version}.dta
qui: drop if _merge!=3
qui: drop _merge
qui: keep log${var_y}* ${dep_var}* ${indep_var}* idtime eventdate
qui: tsset eventdate
qui: gen year = yofd(dofq(eventdate))

preserve
forv n = 1(1)$N {
qui: dfuller ${dep_var}`n', noconstant
qui: gen tstat_y`n'= `r(Zt)'
qui: dfuller ${indep_var}`n', noconstant
qui: gen tstat_x`n'= `r(Zt)'
}
qui: keep tstat*
qui: duplicates drop
qui: gen aux = 1
qui: reshape long tstat_x tstat_y, i(aux) j(amr)
qui: drop aux
save df_new_${version}.dta, replace
restore

if ${version} == 1{
preserve
forv n = 1(1)$N {
qui: dfuller log${var_y}`n', drift
qui: gen tstat_y_`n'= `r(Zt)'
qui: dfuller log${var_y}`n' if year<2005, drift
qui: gen tstat_y_b2005_`n'= `r(Zt)'
qui: dfuller log${var_y}`n' if year>=2005, drift
qui: gen tstat_y_a2005_`n'= `r(Zt)'
qui: dfuller log${var_y}`n', trend
qui: gen tstat_y1_`n'= `r(Zt)'
qui: dfuller log${var_y}`n' if year<2005, trend
qui: gen tstat_y1_b2005_`n'= `r(Zt)'
qui: dfuller log${var_y}`n' if year>=2005, trend
qui: gen tstat_y1_a2005_`n'= `r(Zt)'
}
qui: keep tstat*
qui: duplicates drop
qui: gen aux = 1
qui: reshape long tstat_y_ tstat_y_b2005_ tstat_y_a2005_ tstat_y1_ tstat_y1_b2005_ tstat_y1_a2005_, i(aux) j(amr)
qui: drop aux
save df_new.dta, replace
restore
}

forv n = 1(1)$N {
if ${artest}==1{
if ${transform}==0{
qui: gen aux = log${var_y}`n'
qui: gen ar`n' = 0
qui: gen resid`n'=aux-l.aux
}
else if ${transform}==1{
qui: gen aux = ${dep_var}`n'
qui: reg aux l.aux
qui: mat A = e(b)
qui: gen ar`n' = A[1,1]	
qui: predict resid`n', res	
qui: replace resid`n' = resid`n'+A[1,2]							
}
}

qui: gen roll`n' = sqrt(_pi/2)
qui: replace roll`n' = roll`n'*abs(resid`n')								
qui: drop aux
}

qui: keep  ${indep_var}* roll* eventdate ar* 
qui: reshape long ${indep_var} roll ar, i(eventdate) j(amr)
save instanvolatest${version}.dta, replace

preserve 
qui: by amr, sort: egen volatility_ss=mean(roll)
qui: keep ar volatility_ss amr
qui: duplicates drop
qui: save steadystate${version}.dta, replace
restore

qui: tab eventdate, gen(date_)
qui: gen year = yofd(dofq(eventdate))

qui: xtset amr eventdate
qui: xtreg roll l1.${indep_var} date_*, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef1 = A[1,1]
qui: gen tstat1 = A[1,1]/B[1,1]^.5
est store m1

qui: xtset amr eventdate
qui: gen change_x = ${indep_var}-l1.${indep_var}
qui: xtreg roll l1.${indep_var} change_x date_*, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef2 = A[1,1]
qui: gen tstat2 = A[1,1]/B[1,1]^.5
est store m2

esttab m1 m2 using ${tables}/istantaneousvolat${version}.tex, drop(date_*) label r2 replace star(* 0.10 ** 0.05 *** 0.01)

qui: xtset amr eventdate
qui: xtreg roll l1.${indep_var} date_* if year>=2005, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef3 = A[1,1]
qui: gen tstat3 = A[1,1]/B[1,1]^.5
est store m3

qui: xtset amr eventdate
qui: xtreg roll l1.${indep_var} date_* if year<2005, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef4 = A[1,1]
qui: gen tstat4 = A[1,1]/B[1,1]^.5
est store m4

qui: xtset amr eventdate
qui: xtreg roll l1.${indep_var} change_x date_* if year>=2005, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef5 = A[1,1]
qui: gen tstat5 = A[1,1]/B[1,1]^.5
est store m5

qui: xtset amr eventdate
qui: xtreg roll l1.${indep_var} change_x date_* if year<2005, fe vce(robust)
qui: mat A=e(b)
qui: mat B=e(V)
qui: gen coef6 = A[1,1]
qui: gen tstat6 = A[1,1]/B[1,1]^.5
est store m6

esttab m3 m4 m5 m6 using ${tables}/istantaneousvolat${version}subsamples.tex, drop(date_*) label r2 replace star(* 0.10 ** 0.05 *** 0.01)

preserve
qui: keep coef* tstat*
qui: duplicates drop
qui: save coefficients_${version}.dta, replace
restore
********************************************************************************
* BCA ANALYSIS *****************************************************************
********************************************************************************
di "Figures on peaks and troughs **********************************************"
if ${version}==1|${version}==3|${version}==5{
do ${do}/BCA.do
if ${version}==1{
do ${do}/BCADescriptives.do
}
do ${do}/BCAPlots.do
}
}
********************************************************************************
* Table on instantaneous volatility and industry concentration *****************
********************************************************************************
do ${do}/ControlTable.do
********************************************************************************
* Get input for calibration and city-specific moments **************************
********************************************************************************
do ${do}/TimeInvariantCityChar.do
do $do}/InputCalibrationBeforeD.do
********************************************************************************
* ROBUSTNESS: TABLE ON GRANGER CAUSALITY FOR MULTIPLE SPECIFICATIONS ***********
********************************************************************************
use coefficients_1.dta, clear
gen version = 1
append using coefficients_2.dta
replace version = 2 if version==.
append using coefficients_3.dta
replace version = 3 if version==.
append using coefficients_5.dta
replace version = 5 if version==.
append using coefficients_6.dta
replace version = 6 if version==.
duplicates drop
forv x =1(1)6{
replace coef`x' =. if abs(tstat`x')<${x}
}
reshape long coef tstat, i(version) j(regression)
preserve
gen type = 1 								if version==1 & regression==1
gen label = "BASELINE" 						if version==1 & regression==1
replace type = 2 							if version==2 & regression==1
replace label = "top 99th share not s.a." 	if version==2 & regression==1
replace type = 3 							if version==5 & regression==1
replace label = "top 99th share - HP" 		if version==5 & regression==1
replace type = 4 							if version==1 & regression==5
replace label = "top 99th share - after 2005" 			if version==1 & regression==5
replace type = 5 							if version==1 & regression==6
replace label = "top 99th share - before 2005" 			if version==1 & regression==6
replace type = 6 							if version==3 & regression==1
replace label = "Herfindahl" 				if version==3 & regression==1
keep if label!=""
graph bar coef, bar(1, color(black)) over(label, label(angle(45)) sort(type)) ytitle("") graphregion(color(white)) ytitle("Estimated coefficient")
graph export "$graphs/regressionfinalall.png", replace 
restore
********************************************************************************
* MOBILITY GRAPHS **************************************************************
********************************************************************************
do ${do}/Mobility.do
********************************************************************************
* NARRATIVE EVIDENCE ***********************************************************
********************************************************************************
do ${do}/Narrative.do
********************************************************************************


